package com.iplatform.iplatformuser.privilege.api;

import com.iplatform.iplatformuser.common.ConstantUtil;
import com.iplatform.iplatformuser.common.ResponseMsg;
import com.iplatform.commons.pagination.Pagination;
import com.iplatform.iplatformuser.framework.springmvc.BaseController;
import com.iplatform.iplatformuser.framework.util.UniqueIDGenerator;
import com.iplatform.iplatformuser.privilege.service.inter.ITargetService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iplatform.module.privilege.DwTargetBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 指标管理
 * @author heng
 *
 */
@Api(tags = "页面指标")
@RestController
@RequestMapping("/api/v1/target")
public class ApiTargetController extends BaseController {

	@Autowired
	public ITargetService targetService;


	/**
	 * 获取指标列表
	 *
	 * @param request
	 * @param formMap
	 * @param pagination
	 * @return
	 */
	@ApiOperation(value = "获取指标列表")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetName", value = "指标名称", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetType", value = "指标类型", required = false)})
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public ResponseEntity<PageInfo<DwTargetBean>> list(HttpServletRequest request,
                                                       @RequestParam @ApiParam(hidden = true) Map<String, String> formMap,
                                                       @ModelAttribute @ApiParam(hidden = true, value = "pagination") Pagination pagination) {

		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		List<DwTargetBean> resultList = targetService.query(formMap);
		PageInfo<DwTargetBean> pageInfo = new PageInfo<DwTargetBean>(resultList);

		return new ResponseEntity<>(pageInfo, HttpStatus.OK);
	}

	/**
	 * 查询指标类型下拉列表
	 *
	 * @param dwTargetBean
	 * @return
	 */
	@ApiOperation(value = "获取指标类型下拉列表")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "selectTargetType", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, String>>> selectTargetType(HttpServletRequest request,
                                                                      @ModelAttribute @ApiParam(hidden = true, value = "dwTargetBean") DwTargetBean dwTargetBean) {

		List<Map<String, String>> resul = targetService.selectTargetType();

		return new ResponseEntity<>(resul, HttpStatus.OK);
	}

	/**
	 * 增加
	 *
	 * @param request
	 * @param formMap
	 * @param dwTargetBean
	 * @param session
	 * @return
	 */
	@ApiOperation(value = "增加指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetName", value = "指标名称", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetType", value = "指标类别", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "description", value = "指标说明", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "dataForm", value = "数据来源", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "remark", value = "取数逻辑", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "detailUrl", value = "事实数据页地址", required = false) })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg> addMenu(@RequestParam @ApiParam(hidden = true) Map<String, String> formMap,
                                               @ModelAttribute @ApiParam(hidden = true, value = "dwTargetBean") DwTargetBean dwTargetBean) {

		//TODO 获取当前

		dwTargetBean.setTargetId(UniqueIDGenerator.getUUID());
		dwTargetBean.setCreatedBy("admin");//priManager.getUserCode()
		targetService.insert(dwTargetBean);

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}

	/**
	 * 更新
	 * @param formMap
	 * @param dwTargetBean
	 * @return
	 */
	@ApiOperation(value = "更新指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetId", value = "ID", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetName", value = "指标名称", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetType", value = "指标类别", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "description", value = "指标说明", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "dataForm", value = "数据来源", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "remark", value = "取数逻辑", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "detailUrl", value = "事实数据页地址", required = false) })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg> updateMenu(@RequestParam @ApiParam(hidden = true) Map<String, String> formMap,
                                                  @ModelAttribute @ApiParam(hidden = true, value = "dwTargetBean") DwTargetBean dwTargetBean) {
		//TODO 获取当前用户

		dwTargetBean.setUpdatedBy("admin");
		targetService.update(dwTargetBean);

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}

	/**
	 * 删除
	 *
	 * @param request
	 * @param deleteIds
	 * @return
	 */
	@ApiOperation(value = "删除指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "deleteIds", value = "ID集合，用逗号隔开", required = true) })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	public ResponseEntity<ResponseMsg> batchDelete(@RequestParam @ApiParam(hidden = true) String deleteIds) {

		String[] split = deleteIds.split(",");
		targetService.batchDelete(split);

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}

	/**
	 * 获取和专题绑定的重点指标
	 *
	 * @param request
	 * @param formMap
	 * @return
	 */
	@ApiOperation(value = "获取和专题绑定的重点指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "menuId", value = "菜单ID", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetName", value = "菜单名称", required = false)
	})
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "getSelectedTargetList", method = RequestMethod.POST)
	public ResponseEntity<List<DwTargetBean>> getSelectedTargetList(
			@RequestParam @ApiParam(hidden = true) Map<String, String> formMap) {

		List<DwTargetBean> targetList = targetService.querySelectedTargetByMenuId(formMap);

		return new ResponseEntity<>(targetList, HttpStatus.OK);
	}

	@ApiOperation(value = "获取未和专题绑定的重点指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "menuId", value = "菜单ID", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetName", value = "菜单名称", required = false)
			})
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "getNotSelectedTargetList", method = RequestMethod.POST)
	public ResponseEntity<List<DwTargetBean>> getNotSelectedTargetList(
			@RequestParam @ApiParam(hidden = true) Map<String, String> formMap) {

		List<DwTargetBean> targetList = targetService.queryNotSelectedTargetByMenuId(formMap);


		return new ResponseEntity<>(targetList, HttpStatus.OK);
	}

	/**
	 * 获取和专题绑定的全部重点指标
	 *
	 * @param request
	 * @param formMap
	 * @return
	 */
	@ApiOperation(value = "获取和专题绑定的全部重点指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "menuId", value = "Id", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "roleId", value = "角色Id", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetId", value = "指标Id", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetName", value = "指标名称", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetType", value = "指标类别", required = false) })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "selectTargetPage", method = RequestMethod.POST)
	public ResponseEntity<PageInfo<DwTargetBean>> selectTargetPage(
			@RequestParam @ApiParam(hidden = true) Map<String, String> formMap,
			@ModelAttribute @ApiParam(hidden = true, value = "pagination") Pagination pagination) {

		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		List<DwTargetBean> resultList = targetService.queryRoleTargets(formMap);

		PageInfo<DwTargetBean> pageInfo = new PageInfo<DwTargetBean>(resultList);

		return new ResponseEntity<>(pageInfo, HttpStatus.OK);
	}

	/**
	 * 根据专题获取重点指标
	 *
	 * @param request
	 * @param formMap
	 * @return
	 */
	@ApiOperation(value = "绑定指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetId", value = "指标Id", required = true),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "menuId", value = "菜单Id", required = true) })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "bindTarget", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg> bindTarget(@RequestParam @ApiParam(hidden = true) Map<String, String> formMap) {
		if (StringUtils.isEmpty(formMap.get("targetId"))) {
			formMap.put("targetId", "");
		}
		//TODO 获取当前用户

		targetService.selectTarget(formMap.get("targetId"), formMap.get("menuId"), "wangxi");

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}

	/**
	 * 批量绑定指标
	 *
	 * @param request
	 * @param formMap
	 * @return
	 */
	@ApiOperation(value = "批量绑定指标")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "targetIds", value = "指标Id集合，逗号分隔", required = false),
			@ApiImplicitParam(dataType = "String", paramType = "query", name = "menuId", value = "菜单Id", required = true) })
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
		@ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范") })
	@RequestMapping(value = "bindTargets", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg> bindTargets(@RequestParam @ApiParam(hidden = true) Map<String, String> formMap) {
		formMap.put("createBy", "admin");

		targetService.insertTargetMenu(formMap);

		return new ResponseEntity<>(new ResponseMsg(ConstantUtil.SUCCESS), HttpStatus.OK);
	}

}
